package com.cdivtc.management.utils;


import com.artofsolving.jodconverter.*;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.AbstractOpenOfficeDocumentConverter;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.Date;
import java.util.regex.Pattern;

@Component
public class Office2PDF {

    /**
     * 将Office文档转换为PDF. 运行该函数需要用到OpenOffice, OpenOffice下载地址为
     * http://www.openoffice.org/
     *
     * <pre>
     * 方法示例:
     * String sourcePath = "F:\\office\\source.doc";
     * String destFile = "F:\\pdf\\dest.pdf";
     * Converter.office2PDF(sourcePath, destFile);
     * </pre>
     * <p>
     * <p>
     * 源文件, 绝对路径. 可以是Office2003-2007全部格式的文档, Office2010的没测试. 包括.doc,
     * .docx, .xls, .xlsx, .ppt, .pptx等. 示例: F:\\office\\source.doc
     * <p>
     * 目标文件. 绝对路径. 示例: F:\\pdf\\dest.pdf
     *
     * @return 操作成功与否的提示信息. 如果返回 -1, 表示找不到源文件, 或url.properties配置错误; 如果返回 0,
     * 则表示操作成功; 返回1, 则表示转换失败
     */
    public static void Word2Pdf(String srcPath, String desPath) throws IOException {
        File input = new File(srcPath);
        File out = new File(desPath);
        // window 使用  调用openoffice服务线程     本机C盘！！！
        String command = getOfficeHome();
        //Linux使用
        if (command.equals(""))
            return;
        Process p = Runtime.getRuntime().exec(command);

        // 连接openoffice服务
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(
                "127.0.0.1", 8100);
        connection.connect();
        // 转换word到pdf
        DocumentConverter converter = new OpenOfficeDocumentConverter(
                connection);


        converter.convert(input, out);
        // 关闭连接
        connection.disconnect();
        // 关闭进程
        p.destroy();
        System.out.println("转换完成！");
    }


    public static String getOfficeHome() {
        String osName = System.getProperty("os.name");
        System.out.println("操作系统名称:" + osName);
        if (Pattern.matches("Linux.*", osName)) {
            return "/opt/openoffice4/program/soffice -headless -accept=\"socket,host=127.0.0.1,port=8100;urp; -nofirststartwizard\"";
        } else if (Pattern.matches("Windows.*", osName)) {
            return "C:\\Program Files (x86)\\OpenOffice 4\\program\\soffice -headless -accept=\"socket,host=127.0.0.1,port=8100;urp; -nofirststartwizard\"";
        } else if (Pattern.matches("Mac.*", osName)) {
            return "/Applications/OpenOffice.org.app/Contents/";
        }
        return "";
    }


    public ByteArrayOutputStream Word2Pdf(InputStream inputStream, String filenam) throws IOException {

        // window 使用  调用openoffice服务线程   获取不通操作系统对应的路径
        String command = getOfficeHome();
        //Linux使用
        if (command.equals(""))
            return null;
        Process p = Runtime.getRuntime().exec(command);
        // 连接openoffice服务
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(
                "127.0.0.1", 8100);
        connection.connect();
        // 转换word到pdf
        DocumentConverter converter = new OpenOfficeDocumentConverter(
                connection);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        DocumentFormatRegistry documentFormatRegistry = new DefaultDocumentFormatRegistry();
        String extension = FilenameUtils.getExtension(filenam);
        DocumentFormat inputdoc = documentFormatRegistry.getFormatByFileExtension(extension);
        DocumentFormat outpdf = documentFormatRegistry.getFormatByFileExtension("pdf");
        converter.convert(inputStream, inputdoc, outputStream, outpdf);
        // 关闭连接
        connection.disconnect();
        // 关闭进程
        p.destroy();
        return outputStream;
    }


    public static void main(String[] args) throws IOException {


        Date date = new Date();
        Word2Pdf("D:\\test/任务4-用户和用户组.doc","D:\\test\\任务4-用户和用户组.pdf");
        InputStream inputStream = new FileInputStream(new File("D:\\test\\任务4-用户和用户组.pdf"));
//        pdf2png(inputStream,"png");
        System.out.println("(new Date().getTime()-date.getTime()) = " + (new Date().getTime() - date.getTime()));
    }


}